package com.bluefish.bltuan.listener;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.context.WebApplicationContext;

import com.bluefish.bltuan.module.mail.EmailSetting;
import com.bluefish.bltuan.module.mail.Subscriber;
import com.bluefish.bltuan.service.EmailService;
import com.bluefish.bltuan.service.EmailSettingService;
import com.bluefish.bltuan.service.SubscriberService;
import com.bluefish.bltuan.service.impl.EmailServiceImpl;
import com.bluefish.bltuan.tools.DateUtils;

public class SubscribeListener implements ServletContextListener{

	private ApplicationContext context;
	
	private JavaMailSender javaMailSender;
	
	private EmailService emailService;
	
	private String subject = "聚点团购";
	
	private static final long PERIOD_DAY = 1000 * 60 * 60 * 24;
	
	private Timer timer;
	
	public void contextDestroyed(ServletContextEvent sce) {
		context = null;
		javaMailSender = null;
		emailService = null;
	}

	public void contextInitialized(ServletContextEvent sce) {
		context = (ApplicationContext) sce.getServletContext().getAttribute(
				WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
		initJavaMainSender();
		initEmailService();
		initTimer();
	}
	
	private void initTimer() {
		timer = new Timer("send email", true);
		timer.schedule(new SubscribeTask(), getNextDay(), PERIOD_DAY);
	}

	private Date getNextDay() {
		return DateUtils.getTomorrow();
	}

	private class SubscribeTask extends TimerTask{

		@Override
		public void run() {
			sendEmailToSubscribers();
		}
		
	}
	
	private void sendEmailToSubscribers() {
		SubscriberService subscriberService = getBean("subscriberService");
		List<Subscriber> subList = subscriberService.listAll();
		List<String> tos = new ArrayList<String>();
		for(Subscriber subscriber:subList){
			tos.add(subscriber.getEmail());
		}
		emailService.sendEmail("boventech.com@gmail.com",tos, subject,"团购是一种生活方式！");
	}

	private void initEmailService() {
		this.emailService = new EmailServiceImpl(this.javaMailSender);
	}

	private void initJavaMainSender() {
		EmailSettingService emailSettingService = getBean("emailSettingService");
		EmailSetting setting = emailSettingService.getEmailSetting();
		JavaMailSenderImpl temp = new JavaMailSenderImpl();
		if(setting != null){
			this.subject = setting.getSubject();
			temp.setUsername(setting.getUsername());
			temp.setPassword(setting.getPassword());
			temp.setPort(setting.getSmtpPort());
			temp.setHost(setting.getSmtp());
			Properties properties = getProperties(setting);
			temp.setJavaMailProperties(properties);
		}
		this.javaMailSender = temp;
	}

	private Properties getProperties(EmailSetting setting) {
		Properties properties = new Properties();
		properties.setProperty("mail.smtp.host", setting.getSmtp());
		properties.setProperty("mail.smtp.port", String.valueOf(setting.getSmtpPort()));
		properties.setProperty("mail.smtp.auth", String.valueOf(setting.isSmtpAuth()));
		properties.setProperty("mail.smtp.starttls.enable", String.valueOf(setting.isSmtpTlsEnable()));
		properties.setProperty("mail.smtp.starttls.required", String.valueOf(setting.isSmtpTlsRequired()));
		properties.setProperty("mail.smtp.socketFactory.class", setting.getSmtpSocketClass());
		properties.setProperty("mail.smtp.socketFactory.fallback", String.valueOf(setting.isSmtpSocketFallback()));
		properties.setProperty("mail.smtp.socketFactory.port", String.valueOf(setting.getSmtpSocketPort()));
		return properties;
	}

	@SuppressWarnings("unchecked")
	protected<T> T getBean(String beanName) {
		return (T)this.context.getBean(beanName);
	}

}
